using System.Collections.Generic;
using System.Linq;
using Magiq.Behaviors;

namespace Magiq.Query
{
    public class Query<T> : BehaviorHandler<IQueryBehavior>, IQuery<T> where T : class
    {
        public Query(IEnumerable<T> enumerable, IMagiqBehavior behavior) : base(behavior)
        {
            Source = enumerable;
            Default<DefaultQueryBehavior>();
        }

        public IQueryable<T> Execute()
        {
            var b = Behavior<IQueryBehavior>();
            var strategy = b.Call(x => x.GetQueryStrategy(this));
            return b.Call(x => x.Execute(strategy));
        }

        public IEnumerable<T> Source { get; private set; }

        IMagiqBehavior IBehaviorBasedExecution.Behavior
        {
            get { return BehaviorChain; }
        }
    }
}